<?php

class Manager_model extends MY_CI_Model
{
    /**
     *
     * @access public
     *
     */
    public function __construct()
    {
        $this->table        = 'users';
        $this->primary_id   = 'id';
        $this->primary_name = 'mobile';

        parent::__construct();
        $this->mobile       = $this->session->userdata('mobile');
        $this->gender_lable = array(0 => '保密', 1 => '女', 2 => '男');
    }


    /**
     * 修改数据
     * @param unknown $datas
     * @param unknown $primary_id
     * @return multitype:number string
     */
    public function edit_data($datas, $primary_id, $where_in = FALSE)
    {
        try {
            //移除mobile更新先
            unset($datas['mobile']);
            $db_data = $this->check_post_form_datas($datas);
            if (!empty($db_data)) {
                $this->db->where('id', $_SESSION['user_id']);
                $this->db->update($this->table, $db_data);
                $this->result[$this->primary_id] = $primary_id;
                $log_info                        = sprintf("[%s]修改或删除管理员信息成功！", $this->mobile);
            } else {
                $log_info                = sprintf("[%s]修改或删除管理员信息 未更新数据！", $this->mobile);
                $this->result['error']   = 1;
                $this->result['message'] = '未更新任何数据';
            }
        } catch (exception $e) {
            $log_info                = sprintf("[%s]修改或删除管理员信息失败！", $this->mobile);
            $this->result['error']   = 1;
            $this->result['message'] = $e->getMessage();
        }
        return $this->result;
    }

    /**
     * 查询条件
     * @param array $filter
     * @return void
     */
    function list_query_filter($filter = array())
    {
        if (isset($filter['is_validated']) && $filter['is_validated'] > 0) {
            $this->db->where('a.is_validated', $filter['is_validated'] - 10);
        }
        $is_delete = isset($filter['trash']) && $filter['trash'] > 0 ? 1 : 0;
        $this->db->where('a.is_delete', $is_delete);

        if (isset($filter['keywords']) && !empty($filter['keywords'])) {
            $where = '(a.' . $this->primary_name . ' like "%' . $filter['keywords'] . '%" ' . ' or a.nickname like "%' . $filter['keywords'] . '%"' . ' or a.email like "%' . $filter['keywords'] . '%"' . ' or a.mobile like "%' . $filter['keywords'] . '%"' . 'or a.admin_name like "%' . $filter['keywords'] . '%")';
            $this->db->where($where);
        }
    }


    /**
     * 获取分页数据
     * @param array $filter
     * @param number $page
     * @param number $page_size
     * @return multitype:Ambigous <multitype:, number> multitype:string
     */
    public function get_list($filter = array(), $page = 1, $page_size = 15)
    {
        $arr = array();
        //初始化条件
        if (isset($filter['sort']) && !empty($filter['sort'])) {
            $filter['sort'] = trim($filter['sort']);
            if (!$this->check_single_field($filter['sort'])) {
                $filter['sort'] = $this->primary_id;
            }
        } else {
            $filter['sort'] = $this->primary_id;
        }
        $filter['order'] = isset($filter['order']) && !empty($filter['order']) ? trim($filter['order']) : 'desc';
        //获取记录数
        $record_count = $this->get_list_count($filter);
        $pager        = $this->page_and_size($filter, $record_count, $page, $page_size);

        if ($pager['record_count'] > 0) {
            $this->db->select('a.*');
            $this->db->from($this->table . ' as a');
            $this->list_query_filter($filter);
            $this->query_list_sort_order($filter);
            $this->db->limit($pager['page_size'], $pager['start']);
            $item_list = $this->db->get()->result_array();
            foreach ($item_list as $row) {
                $row['format_last_time'] = empty($row['last_time']) ? '' : local_date("Y-m-d H:i", $row['last_time']);
                $row['gender_name']      = $this->gender_lable[$row['gender']];
                $row['priv_name']        = "";//$this->priv_name($row['action_list']);
                $arr[]                   = $row;
            }
        }
        return array('list' => $arr, 'pager' => $pager);
    }

    /**
     * 取得详细信息
     * @param unknown $store_id
     * @return string
     */
    public function get_info($admin_id, $fields = '*')
    {
        $this->db->select('a.*');
        $this->db->from($this->table . ' as a');
        $this->db->where('a.id', $admin_id);
        $infos                     = $this->db->get()->row_array();
        $infos['format_last_time'] = empty($infos['last_time']) ? '' : local_date("Y-m-d H:i", $infos['last_time']);
        $infos['gender_name']      = $this->gender_lable[$infos['gender']];


        $info['dm_name'] = '没有负责的模块';
        if (isset($info['module_list']) && $info['module_list'] != '') {
            $mod_arr  = explode(',', $info['module_list']);
            $mod      = array('canteen' => '订餐模块', 'express' => '快递模块', 'washing' => '洗车模块');
            $mod_item = array();
            foreach ($mod_arr as $item) {
                $mod_item[] = $mod[$item];
            }
            $info['dm_name'] = implode('|', $mod_item);
        }

        $infos['priv_name'] = "";
        return $infos;
    }
    //负责模块名称


    /**
     * 新增数据
     * @param unknown $datas
     */
    public function add_data($datas)
    {
        try {
            if ($this->checkadminname($datas['admin_name'])) {
                $datas['last_time'] = '';
                $datas['add_time']  = date(time());
                $datas['password']  = md5('123456');
                $db_data            = $this->check_post_form_datas($datas);
                if (!empty($db_data)) {
                    $this->db->insert($this->table, $db_data);
                    $this->result[$this->primary_id] = $this->db->insert_id();
                    $log_info                        = sprintf("[%s]新增管理员信息成功！", $this->admin_name);
                } else {
                    $log_info                = sprintf("[%s]新增管理员信息 未写入数据！", $this->admin_name);
                    $this->result['error']   = 1;
                    $this->result['message'] = '未写入任何数据';
                }
            } else {
                $log_info                = sprintf("用户名[%s]已存在！", $this->admin_name);
                $this->result['error']   = 2;
                $this->result['message'] = '对不起，你输入的用户名已被使用';
            }
        } catch (exception $e) {
            $log_info                = sprintf("[%s]新增管理员信息失败！", $this->admin_name);
            $this->result['error']   = 1;
            $this->result['message'] = $e->getMessage();
        }
        add_admin_log($log_action = 'add manager', $this->result[$this->primary_id], $log_info);
        return $this->result;
    }


    /**
     *  彻底删除某admin
     * @param unknown $datas
     * @param unknown $primary_id
     * @return multitype:number string
     */
    public function drop_data($primary_id)
    {
        try {
            $this->db->where('id', $primary_id);
            $this->db->delete('helper');

            $log_info = sprintf("[%s]彻底删除管理员 成功", $this->admin_name);
        } catch (exception $e) {
            $log_info = sprintf("[%s]彻底删除管理员失败！", $this->admin_name);
        }
        add_admin_log($log_action = 'drop admin', $primary_id, $log_info);
    }


    //判断用户名是否重复
    public function checkadminname($admin_name)
    {
        $this->db->select('a.id');
        $this->db->from($this->table . ' as a');
        $this->db->where('a.mobile', $admin_name);
        $infos = $this->db->get()->row_array();
        if (empty($infos['id'])) {
            return true; //不存在重复
        } else {
            return false; //存在重复
        }
    }

}

?>